/**
 * Project Name:dsrs-systemservice-service
 * File Name:WeakPwdServiceImpl.java
 * Package Name:com.bwda.dsrs.systemservice.service.impl;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.datadict.DatadictCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.po.datadict.DatadictPo;
import com.bwda.dsrs.systemservice.mapper.DatadictMapper;
import com.bwda.dsrs.systemservice.service.WeakPwdService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 弱密码
 *
 * @author liangjianyong
 * @date 2019/4/26
 */
@Service
public class WeakPwdServiceImpl extends AbstractBaseServiceImpl<DatadictPo, DatadictCondition, DatadictMapper> implements WeakPwdService {
    
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insert(DatadictPo po) {
        Integer nextNum = this.getMapper().queryNextOrder(Constants.WEAK_DICT_TYPE);
        fillDefaultVal(po,nextNum);
        po.setIsDefault(Constants.IS_NOT_DEFAULT);
        this.getMapper().insert(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int update(DatadictPo po) {
        po.setDelFlag(Constants.NOT_DELETED);
        return super.update(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void updateWithValid(DatadictPo po) throws SystemServiceException {
        po.setDelFlag(Constants.NOT_DELETED);
        super.updateWithValid(po);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void delete(Object id) throws SystemServiceException {
        DatadictPo po=new DatadictPo();
        po.setId((Integer) id);
        po.setDelFlag(Constants.DELETED);
        super.update(po);
    }



    /**
     * 批量新增(导入使用)
     * @param datas
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void insertBatch(List<DatadictPo> datas){
        //查询下一个排序值
        Integer nextNum = this.getMapper().queryNextOrder(Constants.WEAK_DICT_TYPE);
        if(nextNum==null){
            nextNum=1;
        }
        //填充默认值
        for(DatadictPo po:datas){
            fillDefaultVal(po,nextNum);
            nextNum++;
            this.getMapper().insert(po);
        }
        //批量新增
    }

    @Override
    public int selectByvalue(DatadictCondition condition) {
        return  this.getMapper().selectByvalue(condition);
    }

    /**
     * 默认值填充
     * @param nextNum
     */
    private void fillDefaultVal(DatadictPo po,Integer nextNum){
        //设置默认参数
        String codeAndNameStr = Constants.WEAK_DICT_TYPE.concat(String.valueOf(nextNum));
        po.setDataDictTypeCode(Constants.WEAK_DICT_TYPE);
        po.setDataDictCode(codeAndNameStr);
        po.setDataDictName(codeAndNameStr);
        po.setRestartEffect(Constants.RestartEffect.FALSE);
        po.setDelFlag(Constants.NOT_DELETED);
        po.setStatus(Constants.ENABLE);
        po.setOrderNum(nextNum);
    }


}
